/*
 * master.c
 *
 *  Created on: Jan 5, 2013
 *      Author: redchen
 */

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <rpc.h>

#include "master_server.h"
#include "master_service.h"

typedef struct  {
    int32_t port;
    int32_t interval;
    int32_t duplicate_num;
    int32_t heart_interval;
    int32_t thread_min_size;
    int32_t thread_max_size;
    int32_t status;
}MasterServer;

static MasterServer* mMasterServerInst = NULL;

/**
 * load config
 */
static int master_server_init()
{
    return 0;
}

void master_service_message(Buffer* input, Buffer* output) {
    Request req = {};
    Response resp = {};
    buffer_to_request(input, &req);
    master_service_request(&req, &resp);
}

/**
 * startup handler thread pool
 */
static void* master_server_work_pool_startup(void* nul)
{
    //Step 2. Startup Master Server
    rpc_serving(
            mMasterServerInst->port,
            100,
            master_service_message
    );
    return NULL;
}

void master_server_start()
{
    if(!master_server_init())
    {
        return;
    }
    pthread_t pool_thread_id;
    pthread_create(&pool_thread_id, NULL, master_server_work_pool_startup, (void*) NULL);
    while (1)
    {
        sleep(100);
    }
}

